package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.game.HateRecorder;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.utils.RdmPropUtil;
import com.myk.game.heroscuffle.utils.WeighterTask;

import java.util.List;

public class Skill_JuLang extends Skill {

    private static final int DAMAGE = 50; //伤害值
    private static final int TEMP_DEF = 20; //防御力临时增加值

    public Skill_JuLang() {
        super("巨浪",
                "对所有敌人造成法术伤害，并临时增加自己大量防御力。",
                4, 3);
    }

    @Override
    protected void onTrigger() {
        game.ui.printMsg("卷起滔天巨浪...");
        game.ui.sleepShortly();
        //对所有敌人造成伤害
        List<Hero> heroList = game.pool.getAllAliveHeroesBy(null, hostHero);
        for (Hero hero : heroList) {
            RdmPropUtil.lotteryExecute(
                    new WeighterTask(3, () -> doSkillDamageEffect(hero, DAMAGE)),
                    new WeighterTask(2, () -> doSkillDamageEffect(hero, DAMAGE + 10)),
                    new WeighterTask(1, () -> doSkillDamageEffect(hero, DAMAGE - 5))
            );
            if (hero.state.isAlive) {
                //hero.setEnemy(hostHero);
                game.recorder.record(game.round,hostHero,hero, HateRecorder.HATE_TYPE_EFFECT);
            }
            game.ui.sleepShortly();
        }
        //临时增加自己的防御力
        doSkillBufferEffect();
    }

    private void doSkillDamageEffect(Hero hero, int damage) {
        game.ui.printlnMsg(hero.getObviousName() + "被水淹没");
        hero.sufferDamage(null, this, null, damage);
    }

    private void doSkillBufferEffect() {
        int defUpTo = hostHero.getDefUpTo();
        if (defUpTo > TEMP_DEF) {
            hostHero.def += TEMP_DEF;
            hostHero.event.onBeforeAction.addRunBack(eventData -> {
                hostHero.def -= TEMP_DEF;
                game.ui.printlnMsg(hostHero.getObviousName() + "def恢复至" + hostHero.def + "点。");
                game.ui.sleepShortly();
                eventData.removeRunBackFromListener();
            });
            game.ui.printlnMsg(hostHero.getObviousName() + "def临时增加" + TEMP_DEF + "点，当前" + hostHero.def + "。");
        }
        else {
            hostHero.def += defUpTo;
            hostHero.event.onBeforeAction.addRunBack(eventData -> {
                hostHero.def -= defUpTo;
                game.ui.printlnMsg(hostHero.getObviousName() + "def恢复至" + hostHero.def + "点。");
                game.ui.sleepShortly();
                eventData.removeRunBackFromListener();
            });
            game.ui.printlnMsg(hostHero.getObviousName() + "def临时增加" + defUpTo + "点，当前" + hostHero.def + "(已达上限)。");
        }
        game.ui.sleepShortly();
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {

    }
}
